package com.chen.integral.module.users.service.impl;

import com.chen.base.module.base.dto.Response;
import com.chen.base.module.base.service.impl.BaseServiceImpl;
import com.chen.dao.UsersDao;
import com.chen.entity.Users;
import com.chen.integral.module.users.service.UsersService;
import com.chen.integral.module.users.vo.UsersQueryVo;
import com.mysql.jdbc.StringUtils;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: manager-spring-mvc-hb
 * @Description: to do
 * @Author: chenjiwei
 * @Date: 2020-11-15 15:05
 * @Version: 1.0.0
 */
@Service
public class UsersServiceImpl extends BaseServiceImpl<Users> implements UsersService<Users> {

//	private Logger log = Logger.getLogger(GoodsServiceImpl.class);

    @Resource
    UsersDao userDao;

    @Override
    public JpaSpecificationExecutor<Users> getBaseRepository() {
        return userDao;
    }


    public void deleteBatch(List<String> ids){
        userDao.deleteBatch(ids);
    }

    @Override
    public void save(Users entity) {
        userDao.save(entity);
    }

    @Override
    public void modify(Users entity) {
        userDao.save(entity);
    }

    protected String filterForFindAll(){
        return "code-like,zhcn-like";
    }

    public Response query(UsersQueryVo vo){
        StringBuilder hql = new StringBuilder();

        hql.append(" select user_id as id, user_name as name, user_age as age ");
        hql.append(" from users where 1 = 1 ");

        Map param = new HashMap<String, Object>();
        if(vo.getId() != null){
            hql.append(" and user_id like :userId ");
            param.put("userId", vo.getId());
        }
        if(!StringUtils.isNullOrEmpty(vo.getName())){
            hql.append(" and user_name like :userName ");
            param.put("userName", "%" + vo.getName() + "%");
        }
        if(vo.getAge() != null){
            hql.append(" and user_age = :userAge ");
            param.put("userAge", vo.getAge());
        }

//        hql.append(" limit  ");

        return this.nativeQueryForMap(hql.toString(), Users.class, param);
    }
}
